#ifndef PRACTICE456_H_INCLUDED
#define PRACTICE456_H_INCLUDED
#include <iostream>
#include<list>
using namespace std;

void listmemset(list<int>& mylist, int n)
{
    for(int i = 0; i < n; ++i)
    {
        mylist.push_back(i);
    }
}


void printlist(list<int>& mylist)
{
    list<int>::iterator itr = mylist.begin();
    for(; itr != mylist.end(); ++itr)
    {

        cout << "x = " << *itr << endl;
    }
}

/*both list should be sorted!*/
void intersection(const list<int> & l1, const list<int> & l2, list<int> & reslist)
{
    list<int>::const_iterator itr1 = l1.begin();
    list<int>::const_iterator itr2 = l2.begin();

    for(; itr1 != l1.end() && itr2 != l2.end(); )
    {
        if(*itr2 == *itr1)
        {
            reslist.push_back(*itr2);
            ++itr1;
            ++itr2;
        }
        else if(*itr2 > *itr1)
        {
            ++itr1;
        }
        else{
            ++itr2;
        }

    }


}



void list_union(const list<int> & l1, const list<int> & l2, list<int> & reslist)
{
    list<int>::const_iterator itr1 = l1.begin();
    list<int>::const_iterator itr2 = l2.begin();

    for(; itr1 != l1.end() && itr2 != l2.end(); )
    {
        if(*itr2 == *itr1)
        {
           // cout << "push " << *itr2 << endl;
            reslist.push_back(*itr2);
            ++itr1;
            ++itr2;
        }
        else if(*itr2 > *itr1)
        {
            reslist.push_back(*itr1);
            ++itr1;

        }
        else{
            reslist.push_back(*itr2);
            ++itr2;

        }



    }
    /*final process*/

    itr1 = l1.end();
    --itr1;
    itr2 = l2.end();
    --itr2;

    if(*itr1 > *itr2)
    {
        reslist.push_back(*itr1);
    }
    else if(*itr1 < *itr2)
    {
        reslist.push_back(*itr2);
    }

}


#endif // PRACTICE456_H_INCLUDED
